package com.sync;

/**
 * 父子继承 锁重入
 * 子类可调用父类的 对象锁
 */
class Main {
    public int i = 10;

    synchronized public void operateMainMethod() {
        try {
            System.out.println("Main method print i = " + i--);
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Sub extends Main {
    synchronized public void operateSubMethod() {
        try {
            while (i > 0) {
                System.out.println("Sub method print i = " + i--);
                operateMainMethod();
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ThreadI extends Thread {
    @Override
    public void run() {
        super.run();
        Sub sub = new Sub();
        sub.operateSubMethod();
    }
}

/**
 * @author bysun
 */
public class Demo06 {
    public static void main(String[] args) {
        ThreadI threadI = new ThreadI();
        threadI.start();
    }
}
